package com.itheima.reggie.controller;


import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;


    /*添加购物车*/
    @PostMapping("/cart/add")
    public ResultInfo add(@RequestBody Cart cart) {
        Cart cartFromDB = cartService.add(cart);
        //前端需要知道数据库中有几份当前订单 （前端的加减号就是当订单大于一时才会出现）
        return ResultInfo.success(cartFromDB);
    }

    /*
     * 查询购物车
     * */
    @GetMapping("/cart/list")
    public ResultInfo cartList() {
        List<Cart> list = cartService.findList();
        return ResultInfo.success(list);
    }

    @PostMapping("/cart/sub")
    public ResultInfo update(@RequestBody Cart cart){

        Cart cart1 = cartService.update(cart);
        return ResultInfo.success(cart1);
    }

    /*清空购物车*/
    @DeleteMapping("/cart/clean")
    public ResultInfo clean(){
        cartService.clean();
        return ResultInfo.success(null);
    }
}
